package cn.chinaunicom.admin.controller;

import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.chinaunicom.admin.entity.AppServer;
import cn.chinaunicom.admin.entity.bo.RgstBO;
import cn.chinaunicom.admin.entity.form.AppForm;
import cn.chinaunicom.admin.entity.form.RgstForm;
import cn.chinaunicom.admin.service.IAppServerService;
import cn.chinaunicom.fast.common.exception.AuthErrorType;
import cn.chinaunicom.fast.common.exception.BaseException;
import cn.chinaunicom.fast.common.utils.MyCache;
import cn.chinaunicom.fast.common.utils.Result;
import lombok.extern.slf4j.Slf4j;

/**
 * 应用服务管理
 * @ProjectName: esay-secret-server
 * @Package: cn.chinaunicom.admin.controller
 * @ClassName: AppController
 * @Description: 应用服务管理
 * @Author: wangjc
 * @CreateDate: 2023/11/24
 * @UpdateUser: wangjc
 * @UpdateDate: 2023/11/24
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("app")
public class AppController {

    @Autowired
    private IAppServerService appService;

    /**
     * 获取应用token
     *
     * @param form
     * @return
     */
    @PostMapping("getAccessToken")
    public Result<String> getAccessToken(@RequestBody AppForm form){
        if(Objects.isNull(form.getAppId()) || Objects.isNull(form.getSecretKey())) {
            log.error("入参不能为空");
            throw new BaseException(AuthErrorType.USER_PSWD_ERROR);
        }

        AppServer app = new AppServer();
        BeanUtils.copyProperties(form, app);
        return Result.success(appService.getAccessToken(app));
    }

    /**
     * 新增
     * @param app
     * @return
     */
    @PostMapping("add")
    public Result<AppServer> add(@RequestBody AppServer app) {
        return Result.success(appService.add(app));
    }

    /**
     * 关闭应用
     *
     * @param form
     * @return
     */
    @PostMapping("turnOff")
    public Result<?> disabled(@RequestBody AppForm form) {
        if(Objects.isNull(form.getAppId()) || Objects.isNull(form.getSecretKey())) {
            log.error("入参不能为空");
            throw new BaseException(AuthErrorType.USER_PSWD_ERROR);
        }
        AppServer app = new AppServer();
        BeanUtils.copyProperties(form, app);
        appService.disabeld(app);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param app
     * @return
     */
    @PostMapping("upd")
    public Result<?> upd(@RequestBody AppServer app){
        return Result.success(this.appService.upd(app));
    }

    /**
     * 获取详情
     *
     * @param form
     * @return
     */
    @PostMapping("get")
    public Result<?> get(@RequestBody AppForm form){
        AppServer app = new AppServer();
        BeanUtils.copyProperties(form, app);
        return Result.success(appService.get(app));
    }

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("listAll")
    public Result<List<AppServer>> listAll() {
        return Result.success(this.appService.getAll());
    }

    /**
     * 清理缓存 - 根据缓存名称
     * @param keyName
     * @return
     */
    @GetMapping("clearCache")
    public Result<?> clearCache(String keyName){
        MyCache.getInstance().delCache(keyName);
        return Result.success();
    }

    /**
     * 一键注册应用
     * @param form
     * @return
     */
    @PostMapping("rgst")
    public Result<RgstBO> register(@RequestBody RgstForm form){
        log.debug("register form: {}", JSONObject.toJSONString(form));
        return Result.success(this.appService.register(form));
    }
}
